<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Request;
use think\Validate;
use app\common\model\Message as MessageModel;

class Message extends Api
{
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    public function init()
    {
        $this->success('获取成功', ['token' => Request::instance()->token()]);
    }

    public function form()
    {
//        屏蔽验证码
//        $code = $this->request->post('code');
//        if (!captcha_check($code)){
//            $this->error('验证码错误');
//        }

        $data = [
//            'name' => $this->request->post('name'),
//            'phone' => $this->request->post('phone'),
//            'title' => $this->request->post('title'),
            'content' => $this->request->post('content'),
        ];

        $rule = [
//            'name' => 'require|max:20',
//            'phone' => ['require','regex:/^((1[3-9]\d{9})|(0\d{2,3}-\d{7,8}))$/'],
//            'title' => 'require',
            'content' => 'require',
        ];

        $msg = [
//            'name.require' => '请填写姓名',
//            'name.max' => '姓名最大20个字',
//            'phone.require' => '缺少参数tel',
//            'phone.regex' => '联系电话格式错误',
//            'company.require' => '请填写公司',
//            'title.require' => '请填写标题',
            'content.require' => '请填写留言内容',
        ];

        $validate = new Validate($rule, $msg);
        if (!$validate->check($data)) $this->error($validate->getError());
        $check = MessageModel::where(['content'=>$data['content']])->count();
        if ($check > 0){
            $this->error('请勿重复提交');
        }

//        $data['uid'] = $this->auth->id;
        $model_form = new MessageModel();
        $model_form->data($data);
        $model_form->save();
        $this->success('提交成功');
    }
}